package org.itzixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.base.BaseInfoProperties;
import org.itzixi.mapper.*;
import org.itzixi.pojo.Comment;
import org.itzixi.pojo.FriendCircle;
import org.itzixi.pojo.FriendCircleLiked;
import org.itzixi.pojo.Users;
import org.itzixi.pojo.bo.CommentBO;
import org.itzixi.pojo.bo.FriendCircleBO;
import org.itzixi.pojo.vo.CommentVO;
import org.itzixi.pojo.vo.FriendCircleVO;
import org.itzixi.service.CommentService;
import org.itzixi.service.FriendCircleService;
import org.itzixi.service.UsersService;
import org.itzixi.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CommentServiceImpl extends BaseInfoProperties implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private CommentMapperCustom commentMapperCustom;
    @Transactional
    @Override
    public CommentVO createComment(CommentBO commentBO) {
        // 1. 保存评论
        Comment pendingComment = new Comment();
        BeanUtils.copyProperties(commentBO, pendingComment);
        pendingComment.setCreatedTime(LocalDateTime.now());
        commentMapper.insert(pendingComment);
        // 2. 刷新获取评论
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(pendingComment, commentVO);

        Users commentUser = usersService.getById(commentBO.getCommentUserId());
        commentVO.setCommentUserNickname(commentUser.getNickname());
        commentVO.setCommentUserFace(commentUser.getFace());
        commentVO.setCommentId(pendingComment.getId());

        return commentVO;
    }

    @Override
    public List<CommentVO> queryAll(String friendCircleId) {
        Map<String, Object> map = new HashMap<>();
        map.put("friendCircleId", friendCircleId);
        return commentMapperCustom.queryFriendCircleComments(map);
    }

    @Transactional
    @Override
    public void deleteComment(String commentId, String commentUserId, String friendCircleId) {
        QueryWrapper<Comment> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("id", commentId);
        deleteWrapper.eq("comment_user_id", commentUserId);
        deleteWrapper.eq("friend_circle_id", friendCircleId);
        commentMapper.delete(deleteWrapper);
    }
}
